import 'dart:convert';

import 'package:flutter/material.dart';
import '../../common/ScreenAdaper.dart';
import 'package:dio/dio.dart';
import '../../config/Config.dart';
import '../../widget/TextInput.dart';
import '../../widget/Tool.dart';
import '../../services/Storage.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _username;
  String _password;

  // 登录
  _loginPost() async{
    var apiUrl = "${Config.domain}api/book/login";
    Response result=await Dio().post(apiUrl,data:{
      "username":_username,
      "password":_password,
    });
    var list=result.data;
    print(list);
    Tool.ShowToast(list["msg"]);
    if(list['code'] == 1){
      Storage.setString("token", list['data']['Username']);// 以后改成jwt返回，这里测试开发
      Storage.setString("userInfo", json.encode(list['data']));
      Navigator.pushNamed(context, "/index");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: Text("登录"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _logo(),
            SizedBox(height: 30,),
            Container(
              padding: EdgeInsets.all(S.width(20)),
              child: TextInput(
                text: "请输入用户名",
                onChanged: (value){
                  this._username = value;
                },
              ),
            ),
            Container(
              padding: EdgeInsets.all(S.width(20)),
              child: TextInput(
                text: "请输入密码",
                password: true,
                onChanged: (value){
                  this._password = value;
                },
              ),
            ),
            _login(),
            _register(),
          ],
        ),
      ),
    );
  }

  Widget _logo(){
    return Container(
      margin: EdgeInsets.only(top: S.height(100)),
      width: S.getScreenWidth(),
      alignment:Alignment.topCenter,
      child: Image.asset("images/user.png", fit: BoxFit.cover),
    );
  }
  Widget _login(){
    return Container(
      margin: EdgeInsets.all(S.width(40)),
      height: S.height(80),
      width: double.infinity,
      child: RaisedButton(
        child: Text('登录',style:TextStyle(color: Colors.white)),
        color: Colors.blue,
        onPressed: () {
          _loginPost();
        },
      ),
    );
  }
  Widget _register(){
    return Container(
      margin: EdgeInsets.only(left: S.width(40),right: S.width(40)),
      height: S.height(80),
      width: double.infinity,
      child: RaisedButton(
        child: Text('没账号 --> 注册'),
        color: Colors.white,
        onPressed: () {
          Navigator.pushNamed(context, "/register");
        },
      ),
    );
  }
}
